package com.csx.micrometer.sentinel.listener;

import com.csx.micrometer.common.context.MonitorContext;
import com.csx.micrometer.sentinel.core.SentinelDegradeState;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationListener;

/**
 * @author cuisongxu
 * @date 2023/2/9 14:45
 */
public class SentinelEnvironmentChangeEventListener implements ApplicationListener<EnvironmentChangeEvent> {

    private final SentinelDegradeState sentinelDegradeState;

    public SentinelEnvironmentChangeEventListener(SentinelDegradeState sentinelDegradeState) {
        this.sentinelDegradeState = sentinelDegradeState;
    }

    @Override
    public void onApplicationEvent(EnvironmentChangeEvent event) {
        sentinelDegradeState.bindTo(MonitorContext.get().getMeterRegistry());
    }
}
